package hongcaosp.app.android.video.player;


import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.bean.Video;
import hongcaosp.app.android.modle.impl.SearchModleImpl;
import hongcaosp.app.android.modle.impl.UserModleImpl;
import hongcaosp.app.android.modle.mi.SearchModle;
import hongcaosp.app.android.modle.mi.UserModle;
import xlj.lib.android.base.toastcompat.ToastManager;

public class LoadVideoProxy implements VideoDataSource {
    private IListView<IVideo> listView;
    private int type;
    private int number;
    private long userId;
    private UserModle userModle;
    private SearchModle searchModle;
    private String content;

    public LoadVideoProxy(IListView<IVideo> listView,int type, int number, long userId) {
        this.listView = listView;
        this.type = type;
        this.number = number;
        this.userId = userId;

        userModle = new UserModleImpl();
        searchModle = new SearchModleImpl();
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public void freshData() {
        number = 1;
        DataCallBack<Pagebean<Video>> callBack = new DataCallBack<Pagebean<Video>>() {
            @Override
            public void onGetData(Pagebean<Video> data) {
                super.onGetData(data);
                boolean loadMore = false;
                if (data.getList() != null && data.getTotal() > data.getList().size() ) {
                    loadMore = true;
                }
                List<Video> videos = data.getList();
                List<IVideo> videoList = new ArrayList<>();
                videoList.addAll(videos);
                listView.freshList(videoList,loadMore);

            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
                listView.freshList(null,false);
            }
        };
        if (type == 1) {
            userModle.userWorks((int) userId, UserToken.getDefault().getToken(), 20, number, callBack);
        } else if (type == 2){
            userModle.userLoves((int) userId, UserToken.getDefault().getToken(), 20, number, callBack);
        } else if (type == 3){
            searchModle.searchVideoResult(UserToken.getDefault().getToken(),content,20,number,callBack);
        }
    }

    @Override
    public void loadMore() {
        number++;
        DataCallBack<Pagebean<Video>> callBack = new DataCallBack<Pagebean<Video>>() {
            @Override
            public void onGetData(Pagebean<Video> data) {
                super.onGetData(data);
                boolean loadMore = false;
                if (data.getList() != null && data.getTotal() > data.getList().size() ) {
                    loadMore = true;
                }
                List<Video> videos = data.getList();
                List<IVideo> videoList = new ArrayList<>();
                videoList.addAll(videos);
                listView.addList(videoList,loadMore);

            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        };
        if (type == 1) {
            userModle.userWorks((int) userId, UserToken.getDefault().getToken(), 20, number, callBack);
        } else if (type == 2){
            userModle.userLoves((int) userId, UserToken.getDefault().getToken(), 20, number, callBack);
        } else if (type == 3){
            searchModle.searchVideoResult(UserToken.getDefault().getToken(),content,20,number,callBack);
        }
    }
}
